package com.light.mytext

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.CountDownTimer
import android.os.PersistableBundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.android.tu.loadingdialog.LoadingDailog
import com.baidu.mobstat.StatService
import com.light.mytext.animation.FogActivity
import com.light.mytext.file.FileActivity
import com.light.mytext.http.HttpActivity
import com.light.mytext.javaknowledge.JavaKnowledgeActivity
import com.light.mytext.kedavoicetotext.VoiceToTextActivity
import com.light.mytext.lock.LockActivity
import com.light.mytext.mvp.MVPActivity
import com.light.mytext.mvvp.MVVPActivity
import com.light.mytext.mycustomview.CustomViewsActivity
import com.light.mytext.sqlite.SQLiteTestActivity
import com.light.mytext.thread.MyThreadActivity
import com.light.mytext.websocket.MyWebSocketActivity
import com.light.mytext.webview.UrlCrawBroke
import com.light.mytext.webview.WebviewActivity
import com.tencent.liteav.login.ui.LoginActivity
import com.yanzhenjie.permission.AndPermission
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_webview.*

class MainActivity : AppCompatActivity() {
    private var time: TimeCount? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
        init_Listener()
        //百度统计
        StatService.start(this);
        Log.i("生命周期==", "MainActivity-onCreate")

        Thread(Runnable {
            UrlCrawBroke.main(null);
        }).start()
    }

    override fun onStart() {
        super.onStart()
        Log.i("生命周期==", "MainActivity-onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.i("生命周期==", "MainActivity-onResume")
    }

    override fun onRestart() {
        super.onRestart()
        Log.i("生命周期==", "MainActivity-onRestart")
    }

    override fun onPause() {
        super.onPause()
        Log.i("生命周期==", "MainActivity-onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.i("生命周期==", "MainActivity-onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("生命周期==", "MainActivity-onDestroy")
    }

    fun init() {

        AndPermission.with(this)
            .permission(
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
            .onGranted {

            }
            .onDenied {
//                finish()
            }
            .start()
//        time = TimeCount(60000, 1000)
//        time!!.start()
    }
    /* 定义一个倒计时的内部类 */
    internal inner class TimeCount(millisInFuture: Long, countDownInterval: Long)//参数依次为总时长,和计时的时间间隔
        : CountDownTimer(millisInFuture, countDownInterval) {
        override fun onFinish() {//计时完毕时触发
//            Thread(Runnable {
//                UrlCrawBroke.main(null);
//            }).start()
        }

        override fun onTick(millisUntilFinished: Long) {//计时过程显示
        }
    }
    fun init_Listener() {
        //自定义View
        tv_custome.setOnClickListener {
            showDialog()
            startActivity(Intent(this, CustomViewsActivity::class.java))
        }
        //线程
        tv_thread.setOnClickListener {
            startActivity(Intent(this, MyThreadActivity::class.java))
        }
        //数据库
        tv_sqlite.setOnClickListener {
            startActivity(Intent(this, SQLiteTestActivity::class.java))
        }
        //MVVP
        tv_mvvp.setOnClickListener {
            startActivity(Intent(this, MVVPActivity::class.java))
        }
        //腾讯音视频
        tv_tencent.setOnClickListener {
            startActivity(Intent(this, LoginActivity::class.java))
        }
        //起雾效果
        tv_wuqi.setOnClickListener {
            startActivity(Intent(this, FogActivity::class.java))
        }
        //OkHttpClient--自封装
        tv_http.setOnClickListener {
            startActivity(Intent(this, HttpActivity::class.java))
        }
        //安卓同步锁的使用
        tv_lock.setOnClickListener {
            startActivity(Intent(this, LockActivity::class.java))
        }
        //文件流的使用
        tv_file.setOnClickListener {
            startActivity(Intent(this, FileActivity::class.java))
        }
        //MVP的使用
        tv_mvp.setOnClickListener {
            startActivity(Intent(this, MVPActivity::class.java))
        }
        //科大讯飞语音转文字
        tv_voicetotext.setOnClickListener {
            startActivity(Intent(this, VoiceToTextActivity::class.java))
        }
        //java知识点
        tv_javakonwledge.setOnClickListener {
            startActivity(Intent(this, JavaKnowledgeActivity::class.java))
        }
        tv_refreshwebview.setOnClickListener {
            startActivity(Intent(this, WebviewActivity::class.java))
        }
        tv_websocket.setOnClickListener {
            startActivity(Intent(this, MyWebSocketActivity::class.java))
        }
    }

    private var mWaitDialog: LoadingDailog? = null
    fun showDialog() {
        val loadBuilder = LoadingDailog.Builder(this)
            .setMessage("加载中...")
            .setCancelable(false)
            .setCancelOutside(false)
        mWaitDialog = loadBuilder.create()
        if (mWaitDialog != null && !mWaitDialog!!.isShowing) {
            mWaitDialog!!.show()
        }
    }

    fun dismiss() {
        if (mWaitDialog!!.isShowing) {
            mWaitDialog!!.dismiss()
        }
    }


    //https://editor.csdn.net/md/?articleId=115934990

}
